<?php
namespace app\common\model;
use think\Model;
use app\common\validate\UserAddress as AddressValidate;

class UserAddress  extends Model {
	
	/**
     * 错误信息
     */
	public $errorInfo;
	
	/**
     * 获取地址列表
     * @param type $ids
     * @param type $v
     * @return type
     */
	public function getAddressList($uid)
    {
    	$model = new self;
		$model = $model->where('uid',$uid);
		$model = $model->where("is_del",0); 
		$model = $model->order('add_time', 'desc');
		return $model->select(); 
    }
			
	/**
     * 添加地址
     * @param type $data
     * @return  true
     */
	public function addAddress($data)
    {
    	//验证
    	$validate = new AddressValidate;
        if ( !$validate->check($data) ) {
        	$this -> errorInfo = $validate->getError();
            return false;
        }
		//保存
		$result = $this->allowField(true)->save($data);
		if(!$result){
			$this -> errorInfo = '异常错误';
			return fasle;
		}

		//判断默认地址
		if($data['is_default'] == 1){
			$this->where('uid',$this->uid)->where('id','<>',$this->id)->update(['is_default'=>0]);
		}
		//返回成功通知
		return true;
    }			
			
	/**
     * 编辑地址
     * @param type $data
     * @return  true
     */
	public function editAddress($id,$data)
    {
    	//验证
    	$validate = new AddressValidate;
        if ( !$validate->check($data) ) {
        	$this -> errorInfo = $validate->getError();
            return false;
        }
		//保存
		$result = $this->allowField(true)->save($data,['id'=>$id]);
		if(!$result){
			$this -> errorInfo = '异常错误';
			return fasle;
		}
		//判断默认地址
		if($this->is_default == 1){
			$this->where('uid',$this->uid)->where('id','<>',$this->id)->update(['is_default'=>0]);
		}
		//返回成功通知
		return true;
    }
	
	/**
     * 删除地址
     * @param type $ids
     * @return  true
     */
	public function delAddress($uid,$ids)
    {
		//更新为删除状态
		$this->where('uid',$uid)->where('id','in',$ids)->update(['is_del'=>1,'is_default'=>0]);
		//返回成功通知
		return true;
    }
	
	/**
     * 默认地址
     * @param type $ids
     * @return  true
     */
	public function defaultAddress($uid,$id)
    {
		$this->where('uid',$uid)->update(['is_default'=>0]);
		$this->where('uid',$uid)->where('id','in',$id)->update(['is_default'=>1]);
		return true;
    }
	
	 /**
     * 设置用户地址查询初始条件
     * @param null $model
     * @param string $prefix
     * @return \think\Model
     */
    public static function userValidWhere($model=null,$prefix = '')
    {
        if($prefix) $prefix .='.';
        $model = $model == null ? (new self()) : $model;
        return $model->where("{$prefix}is_del",0);
    }
	
	/**
     * 获取用户默认收货地址
     * @param $uid 用户uid
     */
    public static function getUserDefaultAddress($uid,$addressId=0)
    {
    	if($addressId==0){
       		return self::userValidWhere()->where('uid',$uid)->where('is_default',1)->find();
    	}else{
        	return self::userValidWhere()->where('uid',$uid)->where('id',$addressId)->find();
    	}
    }
	
}